package com.zbj.springboot.mybatis.config;

import com.zbj.springboot.mybatis.dao.mapper.MapperBeanNameGenerator;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatisMapperScannerConfig
 *
 * @author weigang
 * @create 2018-04-16
 **/
@Configuration
@AutoConfigureAfter(DataSourceConfig.class)
public class MyBatisMapperScannerConfig {

    //===================master==========================
    @Bean(name = "beanNameGeneratorMaster")
    public MapperBeanNameGenerator beanNameGeneratorMaster() {
        MapperBeanNameGenerator mapperBeanNameGenerator = new MapperBeanNameGenerator();
        mapperBeanNameGenerator.setPostfix("");
        return mapperBeanNameGenerator;
    }

    @Bean(name = "mapperScannerConfigurerMaster")
    public MapperScannerConfigurer mapperScannerConfigurerMaster() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.zbj.springboot.mybatis.dao");
        configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryMaster");
        configurer.setNameGenerator(beanNameGeneratorMaster());

        return configurer;
    }

    //===================slave==========================
    @Bean(name = "beanNameGeneratorSlave")
    public MapperBeanNameGenerator beanNameGeneratorSlave() {
        MapperBeanNameGenerator mapperBeanNameGenerator = new MapperBeanNameGenerator();
        mapperBeanNameGenerator.setPostfix("Slave");
        return mapperBeanNameGenerator;
    }

    @Bean(name = "mapperScannerConfigurerSlave")
    public MapperScannerConfigurer mapperScannerConfigurerSlave() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.zbj.springboot.mybatis.dao");
        configurer.setSqlSessionFactoryBeanName("sqlSessionFactorySlave");
        configurer.setNameGenerator(beanNameGeneratorSlave());

        return configurer;
    }
}
